package com.sgcc.ln.pmos.e_ln_home.activity;

import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.flyco.tablayout.SlidingTabLayout;
import com.sgcc.ln.pmos.arouter.home.HomeArouterPath;
import com.sgcc.ln.pmos.common.adapter.HomeTabPagerAdapter;
import com.sgcc.ln.pmos.common.base.BaseActivity;
import com.sgcc.ln.pmos.common.base.IView;
import com.sgcc.ln.pmos.common.http.HttpInterConst;
import com.sgcc.ln.pmos.common.http.HttpRequestUtils;
import com.sgcc.ln.pmos.common.http.MvpNetCallback;
import com.sgcc.ln.pmos.common.utils.AssetsUtil;
import com.sgcc.ln.pmos.common.utils.ListPageUtil;
import com.sgcc.ln.pmos.common.widget.TitleBar;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.e_ln_home.bean.MarketUserBean;
import com.sgcc.ln.pmos.e_ln_home.bean.MembersMarketBean;
import com.sgcc.ln.pmos.e_ln_home.fragment.PowerUsersFragment;
import com.sgcc.ln.pmos.e_ln_home.fragment.SellElectricityCompanyFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 主体信息
 */
@Route(path = HomeArouterPath.HOME_MEMBERSMARKET)
public class MembersMarketActivity extends BaseActivity {

    private TitleBar mTitleBar;
    private TextView mTvLeftNumber;
    private TextView mTvRightNumber;
    private SlidingTabLayout mStlContent;
    private ViewPager mViewpager;
    private final List<Fragment> fragments = new ArrayList<>();
    private HomeTabPagerAdapter homeTabPagerAdapter;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_membersmarket;
    }

    @Override
    protected void initViews() {
        super.initViews();
        findViewByIds();

        String[] titleList = getResources().getStringArray(R.array.membersmarket_title);

        SellElectricityCompanyFragment sellElectricityCompanyFragment = new SellElectricityCompanyFragment();
        PowerUsersFragment powerUsersFragment = new PowerUsersFragment();
        sellElectricityCompanyFragment.setOntotalgetListener(new SellElectricityCompanyFragment.OntotalgetListener() {
            @Override
            public void Ontotalget(int total) {
                mTvLeftNumber.setText(total + "");
            }
        });
//        powerUsersFragment.setOntotalgetListener(new PowerUsersFragment.OntotalgetListener() {
//            @Override
//            public void Ontotalget(int total) {
//                mTvRightNumber.setText(total + "");
//            }
//        });
        fragments.add(sellElectricityCompanyFragment);
        fragments.add(powerUsersFragment);
        homeTabPagerAdapter = new HomeTabPagerAdapter(getSupportFragmentManager(), fragments);
        homeTabPagerAdapter.setmBottomTabNames(titleList);
        mViewpager.setOffscreenPageLimit(2);
        mViewpager.setAdapter(homeTabPagerAdapter);
        mStlContent.setViewPager(mViewpager);
        mViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mTitleBar.setTitle("主体信息");
        mTitleBar.setRightIcon(R.mipmap.search_ic, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                goMainActivity();
                ARouter.getInstance()
                        .build(HomeArouterPath.HOME_MEMBERSMARKET_SEARCH)
                        .navigation();
            }
        });

//        String json = AssetsUtil.readStringFromAssets("membersmarket.json");
//        MembersMarketBean membersMarketBean = GsonUtils.fromJson(json, MembersMarketBean.class);
//
//        mTvLeftNumber.setText(membersMarketBean.getPowerCompanyTable().size() + "");
//        mTvRightNumber.setText(membersMarketBean.getPowerUserTable().size() + "");
        getTotal();

    }

    private void findViewByIds() {
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTvLeftNumber = (TextView) findViewById(R.id.tv_left_number);
        mTvRightNumber = (TextView) findViewById(R.id.tv_right_number);
        mStlContent = (SlidingTabLayout) findViewById(R.id.stl_content);
        mViewpager = (ViewPager) findViewById(R.id.viewpager);

    }


    private void getTotal() {
        Map<String, Object> params = new HashMap<>();
        params.put("pageNum", 1);
        params.put("pageSize", 10);
        params.put("membersName", "");
        params.put("isSign", "");
        params.put("signExpiryDate", "");
        params.put("signSort", "");
        params.put("expirySort", "");
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.HOME_MARKETUSERLIST, new MvpNetCallback<MarketUserBean>(new IView() {
            @Override
            public void showLoading() {

            }

            @Override
            public void showLoading(String msg) {

            }

            @Override
            public void dismissLoading() {

            }

            @NonNull
            @Override
            public Lifecycle getLifecycle() {
                return null;
            }
        }) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
//                getView().queryGoodsListFailed();
            }

            @Override
            public void onResponse(MarketUserBean response, int id) {
                super.onResponse(response, id);
                if (response != null) {
                    if (response.getCode() == 200) {
                        mTvRightNumber.setText(response.getData().getTotal() + "");

                    } else {
                        ToastUtils.showShort(response.getMsg());
                    }
                } else {
                    ToastUtils.showShort("电力用户获取失败");
                }

            }
        });

    }
}
